﻿using HttpClientLite.Configurations.interfaces;
using HttpClientLite.Handlers.interfaces;
using System.Net.Http;

namespace HttpClientLite;

/// <summary>
/// http管理器基类
/// </summary>
public abstract class HttpManagerBase : IDisposable
{
    /// <summary>
    /// HttpManager配置的集合
    /// </summary>
    protected readonly List<IHttpManagerConfiguration> _httpManagerConfiguration;

    /// <summary>
    /// HttpClient配置的集合
    /// </summary>
    protected readonly List<IHttpClientConfiguration> _httpClientConfiguration;

    /// <summary>
    /// HttpRequestMessage配置的集合
    /// </summary>
    protected readonly List<IRequestMessageConfiguration> _requestMessageConfiguration;

    /// <summary>
    /// HttpResponseMessage处理的集合
    /// </summary>
    protected readonly List<IResponseMessageHandler> _responseMessageHandler;

    /// <summary>
    /// HttpManager的异常处理集合
    /// </summary>
    protected readonly List<IHttpManagerExceptionHandler> _httpManagerExceptionHandler;

    /// <summary>
    /// HttpClient
    /// </summary>
    protected HttpClient? httpClient;

    /// <summary>
    /// baseAddress
    /// </summary>
    protected string? baseAddress;

    /// <summary>
    /// 默认实例化
    /// </summary>
    protected HttpManagerBase()
    {
        _httpManagerConfiguration = [];
        _httpClientConfiguration = [];
        _requestMessageConfiguration = [];
        _responseMessageHandler = [];
        _httpManagerExceptionHandler = [];
    }

    /// <summary>
    /// 设置地址的实例化
    /// </summary>
    /// <param name="baseAddress"></param>
    protected HttpManagerBase(string baseAddress) : this()
    {
        BaseAddress = baseAddress;
    }

    /// <summary>
    /// http请求地址
    /// </summary>
    public string? BaseAddress
    {
        get => baseAddress;
        set
        {
            baseAddress = value;

            if (httpClient is not null)
                httpClient.BaseAddress = new Uri(baseAddress!);
        }
    }

    /// <summary>
    /// 是否抛出异常
    /// 默认为false
    /// </summary>
    public bool IsThrowException { get; set; } = false;

    /// <summary>
    /// 释放HttpClient
    /// </summary>
    public void Dispose()
    {
        httpClient?.Dispose();
    }

    /// <summary>
    /// 设置是否抛出异常
    /// </summary>
    /// <param name="isThrow"></param>
    /// <returns></returns>
    public virtual HttpManagerBase SetThrowException(bool isThrow = true)
    {
        IsThrowException = isThrow;
        return this;
    }

    /// <summary>
    /// 添加配置 IHttpManagerConfiguration
    /// </summary>
    /// <param name="configuration"></param>
    /// <returns></returns>
    public virtual HttpManagerBase Configuration(IHttpManagerConfiguration configuration)
    {
        _httpManagerConfiguration.Add(configuration);
        return this;
    }

    /// <summary>
    /// 添加配置 IHttpClientConfiguration
    /// </summary>
    /// <param name="configuration"></param>
    /// <returns></returns>
    public virtual HttpManagerBase Configuration(IHttpClientConfiguration configuration)
    {
        _httpClientConfiguration.Add(configuration);
        return this;
    }

    /// <summary>
    /// 添加配置 IRequestMessageConfiguration
    /// </summary>
    /// <param name="configuration"></param>
    /// <returns></returns>
    public virtual HttpManagerBase Configuration(IRequestMessageConfiguration configuration)
    {
        _requestMessageConfiguration.Add(configuration);
        return this;
    }

    /// <summary>
    /// 添加处理 IResponseMessageHandler
    /// </summary>
    /// <param name="handler"></param>
    /// <returns></returns>
    public virtual HttpManagerBase Handler(IResponseMessageHandler handler)
    {
        _responseMessageHandler.Add(handler);
        return this;
    }

    /// <summary>
    /// 添加处理 IHttpManagerExceptionHandler
    /// </summary>
    /// <param name="handler"></param>
    /// <returns></returns>
    public virtual HttpManagerBase Handler(IHttpManagerExceptionHandler handler)
    {
        _httpManagerExceptionHandler.Add(handler);
        return this;
    }

    /// <summary>
    /// 获取HttpClient
    /// </summary>
    /// <returns></returns>
    public virtual HttpClient GetHttpClient()
    {
        if (httpClient is null)
        {
            httpClient = new HttpClient();

            if (!string.IsNullOrWhiteSpace(BaseAddress))
            {
                httpClient.BaseAddress = new Uri(BaseAddress);
            }
        }

        return httpClient;
    }

    /// <summary>
    /// 异步发送
    /// </summary>
    /// <param name="httpRequest"></param>
    /// <param name="completionOption"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public abstract Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequest, HttpCompletionOption? completionOption = null, CancellationToken? cancellationToken = null);

    /// <summary>
    /// 发送
    /// </summary>
    /// <param name="httpRequest"></param>
    /// <param name="completionOption"></param>
    /// <returns></returns>
    public abstract HttpResponseMessage Send(HttpRequestMessage httpRequest, HttpCompletionOption? completionOption = null);
}